package com.jiaotong.smart.smartjiaotong.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiaotong.smart.smartjiaotong.entity.OperateLog;
import com.jiaotong.smart.smartjiaotong.entity.WarningLog;
import com.jiaotong.smart.smartjiaotong.mapper.OperateLogMapper;
import com.jiaotong.smart.smartjiaotong.mapper.WarningLogMapper;
import com.jiaotong.smart.smartjiaotong.structure.OperateLogStruct.getOperateLogRes;
import com.jiaotong.smart.smartjiaotong.structure.OperateLogStruct.getOperateLogsReq;
import com.jiaotong.smart.smartjiaotong.structure.OperateLogStruct.getWarningLogsReq;
import com.jiaotong.smart.smartjiaotong.structure.OperateLogStruct.getWarningLogsRes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OperateLogService {

    @Autowired
    OperateLogMapper operateLogMapper;

    @Autowired
    WarningLogMapper warningLogMapper;

    // 获取操作日志
    public getOperateLogRes getOperateLogs(getOperateLogsReq req) {
        String options = req.getOptions();
        int page = req.getPage();
        int pageSize = req.getPageSize();

        // 构建页面条件
        Page<OperateLog> pagesOption = new Page<OperateLog>(page, pageSize);
        LambdaQueryWrapper<OperateLog> query = Wrappers.lambdaQuery();
        query.like(OperateLog::getComment, options);
        query.or().like(OperateLog::getName, options);

        IPage<OperateLog> pages = operateLogMapper.selectPage(pagesOption, query);

        getOperateLogRes res = new getOperateLogRes(pages.getRecords(), pages.getTotal(), pages.getPages());
        return res;
    }

    // 获取警告日志
    public getWarningLogsRes getWarningLogs(getWarningLogsReq req) {
        String options = req.getOptions();
        int page = req.getPage();
        int pageSize = req.getPageSize();

        // 构建页面条件
        Page<WarningLog> pagesOption = new Page<>(page, pageSize);
        LambdaQueryWrapper<WarningLog> query = Wrappers.lambdaQuery();
        query.like(WarningLog::getComment, options);

        IPage<WarningLog> pages = warningLogMapper.selectPage(pagesOption, query);

        getWarningLogsRes res = new getWarningLogsRes(pages.getRecords(), pages.getTotal(), pages.getPages());
        return res;
    }

}
